<?php
/*
* @Author: 李明
* @Date: 2021-08-19 13:54:10
 * @LastEditTime: 2022-01-14 17:26:34
 * @LastEditors: 李明
* @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/admin/TagController.php
* @山东壹零贰肆信息科技有限公司
*/

namespace App\Http\Controllers\admin;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\Tag;
use App\Models\User;
use App\Models\UserTag;
use Exception;
use GrahamCampbell\ResultType\Success;
use Illuminate\Http\Request;

class TagController extends Controller
{
    /**
     * @description: 查询标签
     * @param {Request} $request
     * @return {*}
     */
    public function queryTag(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        $pageSize = $all['size'] ?? 15;
        // $tagList = Tag::all();
        $tagList = Tag::Select(
            'name',
            'id'
        )
            ->paginate($pageSize);

        return LMReturn::Success($tagList);
    }
    /**
     * @description: 添加/修改标签
     * @param {Request} $request
     * @return {*}
     */
    public function saveTag(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }

        //接收前端传递过来的值
        $all = $request->all();
        try {
            if (isset($all['id'])) {
                $tag = Tag::where('id', $all['id'])->first();
                $res = $tag->update($all);
                return LMReturn::Success($tag, '标签修改成功');
            } else {
                $tag = Tag::create($all);
                return LMReturn::Success($tag, '添加标签成功');
            }
        } catch (Exception $exception) {
            return LMReturn::Error('请检查标签是否重复');
        }
    }
    /**
     * @description: 删除标签
     * @param {Request} $request
     * @return {*}
     */
    public function deleteTag(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }

        //接收前端传递过来的值
        $all = $request->all();
        try {
            if (isset($all['id'])) {
                $tag = Tag::where('id', $all['id'])->first();
                if (count($tag->users) > 0) {
                    return LMReturn::Error('无法删除，标签绑定了客户');
                }
                $res = $tag->delete();
                return LMReturn::Success($tag, '标签删除成功');
            } else {
                return LMReturn::Error('请检查id是否正确');
            }
        } catch (Exception $exception) {
            return LMReturn::Error('奇怪了，怎么出错了？');
        }
    }
    /**
     * @description: 给客户增加标签
     * @param {Request} $request
     * @return {*}
     */
    public function addUserTag(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        if (isset($all['tag_list'])) {
            $tagList = $all['tag_list'];
        } else {
            return LMReturn::Error('缺少标签');
        }
        if (isset($all['user_list'])) {
            $userList = $all['user_list'];
        } else {
            return LMReturn::Error('缺少用户');
        }
        try {
            foreach ($tagList as $tag_id) {
                $tag = Tag::Where('id', $tag_id)->first();
                $tag->users()->syncWithoutDetaching($userList);
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('网络出错误', 7101);
        }
        return LMReturn::Success([], '添加成功');
    }
    /**
     * @description: 给客户删除标签
     * @param {Request} $request
     * @return {*}
     */
    public function deleteUserTag(Request $request)
    {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        //接收前端传递过来的值
        $all = $request->all();
        if (isset($all['tag_list'])) {
            $tagList = $all['tag_list'];
        } else {
            return LMReturn::Error('缺少标签');
        }
        if (isset($all['user_list'])) {
            $userList = $all['user_list'];
        } else {
            return LMReturn::Error('缺少用户');
        }
        try {
            foreach ($tagList as $tag_id) {
                $tag = Tag::Where('id', $tag_id)->first();
                $tag->users()->detach($userList);
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('网络出错误', 7101);
        }
        return LMReturn::Success([], '删除成功');
    }
}
